# -*- coding: utf-8 -*-
# @Author : gongyu
# @Date   : 2019/4/8
# @File   : EC06_ExtGuarLoanCnt.py
# @E-mail : gongyu@bbdservice.com

'''eof
name:对外担保笔数
code:EC06_ExtGuarLoanCnt_2D
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ_2D
type:常用指标
dataSourceType:
eof'''

'''
Factor=企业征信的借贷交易相关还款责任汇总信息段(EB05A)-借贷交易相关还款责任汇总信息(EB05AH)·责任类型(EB05AD01)为2(保证人/反担保人 )的其他借贷交易账户数(EB05AS03)
                 + 担保交易相关还款责任汇总信息(EB05B)-担保交易相关还款责任汇总信息(EB05BH)·责任类型(EB05BD01)为2(保证人/反担保人 )的账户数(EB05BS02)

'''

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)

    if occurTimes == 1:
        if table == "":
            return ""
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def EC06_ExtGuarLoanCnt():
    data = EC_DSJ_2D['data']
    ##企业征信的担保交易相关还款责任汇总信息
    # 相关还款责任汇总信息   [1..1]
    EBE = getTable(data, "EBE", 1)
    if EBE is None:
        return u'缺失值'
    elif EBE == [{}] or EBE == "":
        return 0
    else:
        # 相关还款责任汇总   [0..1]
        EB05 = getTable(EBE[0], "EB05", 0)
        if EB05 is None:
            return u'缺失值'
        elif EB05 == [{}] or EB05 == "":
            return 0
        else:
            factor1 = []
            factor2 = []
            # 借贷交易相关还款责任汇总信息段   [0..1]
            EB05A = getTable(EB05[0], "EB05A", 0)
            # 担保交易相关还款责任汇总信息段   [0..1]
            EB05B = getTable(EB05[0], "EB05B", 0)
            if EB05A is None or EB05A == "":
                factor1 = None
            elif EB05A == [{}]:
                factor1 = 0
            else:
                # 借贷交易相关还款责任汇总信息   [0..4]
                EB05AH = getTable(EB05A[0], "EB05AH", 0)
                if EB05AH is None or EB05AH == "":
                    factor1 = None
                elif EB05AH == [{}]:
                    factor1 = 0
                else:
                    for i in EB05AH:
                        try:
                            # 责任类型(EB05AD01)为2(保证人\反担保人)的其他借贷交易账户数(EB05AS03)
                            EB05AD01 = i.get("EB05AD01")
                            EB05AS03 = i.get("EB05AS03")
                            if EB05AD01 in null_type_list or EB05AS03 in null_type_list:
                                factor1.append(None)
                            elif EB05AD01 == '2' and isfloat(EB05AS03):
                                factor1.append(float(EB05AS03))
                            else:
                                factor1.append(0)
                        except:
                            factor1.append(None)
                            continue
                    factor1 = filter(lambda x: x is not None, factor1)
                    if len(factor1) == 0:
                        factor1 = None
                    else:
                        factor1 = sum(factor1)
            if EB05B is None or EB05B == "":
                factor2 = None
            elif EB05B == [{}]:
                factor2 = 0
            else:
                # 担保交易相关还款责任汇总信息   [0..4]
                EB05BH = getTable(EB05B[0], "EB05BH", 0)
                if EB05BH is None or EB05BH == "":
                    factor2 = None
                elif EB05BH == [{}]:
                    factor2 = 0
                else:
                    for i in EB05BH:
                        try:
                            # 责任类型(EB05BD01)为2(保证人\反担保人)的账户数(EB05BS02)
                            EB05BD01 = i.get("EB05BD01")
                            EB05BS02 = i.get("EB05BS02")
                            if EB05BD01 in null_type_list or EB05BS02 in null_type_list:
                                factor2.append(None)
                            elif EB05BD01 == '2' and isfloat(EB05BS02):
                                factor2.append(float(EB05BS02))
                            else:
                                factor2.append(0)
                        except:
                            factor2.append(None)
                            continue
                    factor2 = filter(lambda x: x is not None, factor2)
                    if len(factor2) == 0:
                        factor2 = None
                    else:
                        factor2 = sum(factor2)

            if factor1 is None and factor2 is None:
                return u'缺失值'
            elif factor1 is None and factor2 is not None:
                return factor2
            elif factor1 is not None and factor2 is None:
                return factor1
            else:
                return factor1 + factor2

result=EC06_ExtGuarLoanCnt()